package com.itheima.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.entity.Category;
import com.itheima.entity.Movies;
import com.itheima.exception.BusinessException;
import com.itheima.mapper.MoviesMapper;
import com.itheima.service.CategoryService;
import com.itheima.service.CommentService;
import com.itheima.service.MovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
//@Transactional:用来添加事务的,默认是遇到运行期异常和错误的时候才回滚,为了保证数据的一致性,我们要遇到任何异常的时候都回滚,所以就需要加rollBackFor指定异常类型
@Transactional(rollbackFor = Exception.class)
public class MovieServiceImpl implements MovieService {

    @Autowired
    private MoviesMapper moviesMapper;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private CommentService commentService;

    @Override
    public PageInfo findByPage(Integer pageNum, Integer pageSize, Integer cid, String keyword) {
        //1.参数校验
        if (pageNum<=0) {
            pageNum=1;
        }
        if (pageSize<=0) {
            pageSize=5;
        }

        //2.使用pageHelper完成分页查询
        //- PageHelper.startPage(当前页码,每页条数);
        PageHelper.startPage(pageNum,pageSize);

        //- 紧跟着调用mapper查询数据库(这条语句就会自动分页),返回list
        //trim:去掉字符串两端空白 "  沈 腾 "==>"沈 腾"      "    "==>""
        keyword = (keyword==null?null:keyword.trim());
        List<Movies> list = moviesMapper.findList(cid,keyword);

        //- 使用PageInfo构造器封装list,就可以获取总记录数和分页的一些其他参数
        return new PageInfo(list);
    }

    @Override
    public void deleteBatch(Integer[] ids) {
        //if (ids==null || ids.length==0) {
        if(ArrayUtil.isEmpty(ids)){
            //代码运行时遇到了异常,就不往下执行了;除非有异常的代码使用了try--catch机制,这样的话catch中的代码就会执行且还会往后继续执行
            throw new BusinessException("请选择要删除的影视");
        }

        moviesMapper.deleteBatch(ids);

        //新加操作--将这些影视的所有评论也删除掉
        commentService.deleteBatchByMoviesIds(ids);
    }

    @Override
    public Map<String, List<Movies>> findMovieListMap() {
        /*
            map的键时分类名称,value是该分类下的影视list
         */
        //1.先创建map
        Map<String, List<Movies>> map = new HashMap<>();

        //2.查询所有的分类list
        List<Category> categoryList = categoryService.findList();

        //3.遍历分类list,获取每个分类对象(名字和id)
        if (CollUtil.isNotEmpty(categoryList)) {
            for (Category category : categoryList) {
                //3.1 调用movieMapper根据cid查询前六条数据
                PageHelper.startPage(1,6);
                List<Movies> moviesList = moviesMapper.findListByCategoryId(category.getId());

                //3.2 将分类名称作为key,查询出来的movieList作为value放入map中
                map.put(category.getCategoryName(),moviesList);
            }
        }

        //4.将map返回
        return map;
    }

    @Override
    public Movies findById(Integer id) {
        return moviesMapper.findById(id);
    }

    @Override
    public List<Movies> selectByCategoryId(String id) {
        return moviesMapper.selectByCategoryId(id);
    }

    @Override
    public void add(Movies movies) {
        moviesMapper.add(movies);
    }

    @Override
    public void update(Movies movies) {
        moviesMapper.update(movies);
    }
}
